package com.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSON;
import com.dao.IndustryDetailDao;
import com.opensymphony.xwork2.ActionContext;
import com.po.IndustryDetailInfoPO;
@Controller
public class IndustryDetailAction {
	@Autowired
	private IndustryDetailDao industryDetailDao;
	private IndustryDetailInfoPO detailInfoPO;
	private File image;
	private String imageFileName;
	public String show(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		detailInfoPO = industryDetailDao.findDetailInfoById(id);
		
		return "show";
	}
	
	/**
	 * 上传图片
	 */
	public String upImage(){
		try {
			//给上传的图片重新起名字
			String name = imageFileName;
			long date = System.currentTimeMillis();
			String NewName = date + name.substring(name.lastIndexOf(".")); 
			
			//获取ServletContext对象
			ServletContext context = ServletActionContext.getServletContext();
			
			//	给服务器中上传文件
			FileInputStream fis = new FileInputStream(image);
			int t = 0;
			byte[] bys = new byte[1024];
			FileOutputStream fos = new FileOutputStream(context.getRealPath("/") + "newsDetail_image/" + NewName);
			while((t = fis.read(bys)) != -1){
				fos.write(bys, 0, t);
			}
			//关闭流
			fis.close();
			fos.close();
			//服务器响应的数据
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("text/html;charset=utf-8");
			response.getWriter().write(NewName);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	 return null;
	}
	/**
	 * 修改数据时，回显数据，并跳转到修改界面
	 * @return
	 */
	public String preUpdate(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		
		detailInfoPO = industryDetailDao.findDetailInfoById(id);
		
		ActionContext.getContext().put("id", id);
		return "preUpdate";
	}
	
	/**
	 * 修改详细的新闻动态信息
	 * @return
	 */
	public String update(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		int id = Integer.parseInt(request.getParameter("id"));
		HashMap<String, String> map = new HashMap<String, String>();
		detailInfoPO.setId(id);
		try {
			industryDetailDao.updateDetailInfoById(detailInfoPO);
			map.put("code", "EC001");
		} catch (Exception e) {
			map.put("code", "EC002");
		}
		String msg = JSON.toJSONString(map);
		response.setContentType("text/html;charset=utf-8");
		
		try {
			response.getWriter().write(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	
	
	public IndustryDetailInfoPO getDetailInfoPO() {
		return detailInfoPO;
	}

	public void setDetailInfoPO(IndustryDetailInfoPO detailInfoPO) {
		this.detailInfoPO = detailInfoPO;
	}

	public File getImage() {
		return image;
	}

	public void setImage(File image) {
		this.image = image;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}
	
	
}
